package com.java113.spring.demo.controller;

import com.java113.spring.demo.model.UserInfo;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/r1")
    public String r1(String keyword) {
        return "接收参数：" + keyword;
    }

    @RequestMapping("/r2")
    public String r2(String user, String password) {
        return "接收参数：" + user + " " + password;
    }

    @RequestMapping(value = "/r3",method = RequestMethod.GET)
    public String r3(String user, String password) {
        return "接收参数：" + user + " " + password;
    }

    @RequestMapping(value = "/r4",method = RequestMethod.POST)
    public String r4(String user, String password) {
        return "接收参数：" + user + " " + password;
    }

    @PostMapping("/r5")
    public String r5(String user, String password) {
        return "接收参数：" + user + " " + password;
    }

    @GetMapping("/r6")
    public String r6(String user, String password) {
        return "接收参数：" + user + " " + password;
    }

    @RequestMapping("/r7")
    public String r7(UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r8")
    public String r8(@RequestParam(value = "p",required = false) String name , @RequestParam("q") String password) {
        return "p = name = " + name + " q = password = " + password ;
    }

    @RequestMapping("/r9")
    public String r9(String[] arrays) {
        return Arrays.toString(arrays);
    }

    //传递JSON
    @RequestMapping("/r10")
    //@RequestBoyd请求正文 注解作⽤在请求正⽂的数据绑定，请求参数必须在写在请求正⽂中
    public String r10(@RequestBody UserInfo userInfo) {
        return userInfo.toString();
    }

    @RequestMapping("/r11/{articleId}")
    //@PathVariable 路径变量 注解主要作⽤在请求URL路径上的数据绑定
    public String r11(@PathVariable Integer articleId) {
        return "文章ID : " + articleId;
    }

    @RequestMapping("/r12/{articleId}/{aut}")
    public String r12(@PathVariable Integer articleId, @PathVariable("aut") String author) {
        return  "文章ID : " + articleId + " 作者: " + author;
    }

    @RequestMapping("/r13")
    public String r13(@RequestPart("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        //文件上传
        file.transferTo(new File("F:\\homework\\computer\\Java\\资料\\" + file.getOriginalFilename()));
        return "上传成功！";
    }

    //获取Cookie
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        StringBuffer sbf = new StringBuffer();
        if(cookies!= null) {
            for (Cookie cookie : cookies) {
                sbf.append(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return sbf.toString();
    }
    //简洁获取，不常用
    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("bit") String bit) {
        return "从Cookie中获取Java的值：" + bit;
    }

    //存储Session
    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        //从cookie中获取SessionId，根据sessionId获取Session对象
        //如果sessionId不存在则创建
        HttpSession session = request.getSession();
        //默认存储在内存中
        session.setAttribute("name","zhangsan");
        session.setAttribute("age","17");
        return "设置session成功";
    }
    //获取Session-HttpServletRequest
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
//        HttpSession session = request.getSession();//默认getSession为true
        HttpSession session = request.getSession(false);//默认getSession为true

        if(session == null) {
            return "用户未登录";
        }else {
            String name = (String) session.getAttribute("name");
            return "用户登录为：" + name;
        }
    }
    //获取Session-HttpSession
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {
        String name = (String)session.getAttribute("name");
        return "用户登录为：" + name;
    }

    //获取Session-@SessionAttribute()
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name) {
        return "用户登录为：" + name;
    }

    //获取请求头参数
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取userAgent:" + userAgent;
    }
}
